package com.ruoyi.system.service.impl;

import com.ruoyi.common.core.domain.LmTaskTypeVO;
import com.ruoyi.common.core.domain.entity.LmTaskType;
import com.ruoyi.system.mapper.LmTaskTypeMapper;
import com.ruoyi.system.service.ILmTaskTypeService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class LmTaskTypeServiceImpl implements ILmTaskTypeService {

    @Autowired
    private LmTaskTypeMapper taskTypeMapper;

    @Override
    public List<LmTaskTypeVO> selectTaskTypeList(LmTaskType taskType,String taskTypeId) {
        List<LmTaskType> taskTypeList = taskTypeMapper.selectTaskTypeList(taskType,taskTypeId);
        return buildTaskTypeTree(taskTypeList);
    }


    @Override
    public List<LmTaskType> selectTaskTypeListNew(LmTaskType taskType,String taskTypeId) {
        List<LmTaskType> list = taskTypeMapper.selectTaskTypeList(taskType, taskTypeId);

        // 直接转换为VO并返回，不构建树形结构
        return list;
    }

    @Override
    public LmTaskType selectTaskTypeById(Integer id) {
        return taskTypeMapper.selectTaskTypeById(id);
    }

    @Override
    public int insertTaskType(LmTaskType taskType) {
        return taskTypeMapper.insertTaskType(taskType);
    }

    @Override
    public int updateTaskType(LmTaskType taskType) {
        return taskTypeMapper.updateTaskType(taskType);
    }

    @Override
    public int deleteTaskTypeById(Integer id) {
        return taskTypeMapper.deleteTaskTypeById(id);
    }

    @Override
    public List<LmTaskTypeVO> buildTaskTypeTree(List<LmTaskType> taskTypeList) {
        // 获取顶层节点（pid为0或null的节点）
        List<LmTaskType> rootList = taskTypeList.stream()
                .filter(taskType -> taskType.getPid() == null || taskType.getPid() == 0)
                .collect(Collectors.toList());

        // 构建树结构
        List<LmTaskTypeVO> rootVoList = new ArrayList<>();
        for (LmTaskType taskType : rootList) {
            LmTaskTypeVO taskTypeVO = new LmTaskTypeVO();
            BeanUtils.copyProperties(taskType, taskTypeVO);
            taskTypeVO.setChildren(getChildren(taskType.getId(), taskTypeList));
            rootVoList.add(taskTypeVO);
        }

        return rootVoList;
    }

    /**
     * 递归获取子节点
     *
     * @param id 当前节点ID
     * @param taskTypeList 所有节点列表
     * @return 子节点列表
     */
    private List<LmTaskTypeVO> getChildren(Integer id, List<LmTaskType> taskTypeList) {
        List<LmTaskType> children = taskTypeList.stream()
                .filter(taskType -> id.equals(taskType.getPid()))
                .collect(Collectors.toList());

        List<LmTaskTypeVO> childrenVO = new ArrayList<>();
        for (LmTaskType child : children) {
            LmTaskTypeVO childVO = new LmTaskTypeVO();
            BeanUtils.copyProperties(child, childVO);
            childVO.setChildren(getChildren(child.getId(), taskTypeList));
            childrenVO.add(childVO);
        }

        return childrenVO;
    }
}
